<template>
  <div>
    <div style="margin-bottom: 15px">
      <el-input v-model="params.username" style="width: 200px" placeholder="请输入护理记录id"></el-input>
      <el-button type="primary" icon="el-icon-search" style="margin-left: 10px" @click="findSelfInfoByCareID"></el-button>
      <el-button type="warning" icon="el-icon-refresh-right" style="margin-left: 10px" @click="reset"></el-button>
      <el-button type="primary" icon="el-icon-circle-plus-outline" style="margin-left: 10px" @click="addCare">新增</el-button>
    </div>
    <!--用户信息表-->
    <div>
      <el-table :data="tableData" style="width: 100%">
<!--        这里绑定的是实体类里的字段-->
        <el-table-column prop="careId" label="护理id"></el-table-column>
        <el-table-column prop="type" label="类型"></el-table-column>
        <el-table-column prop="score" label="分数"></el-table-column>
        <el-table-column prop="appraise" label="评价"></el-table-column>
        <el-table-column prop="finish" label="是否完成"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" @click="editCare(scope.row)"></el-button>
<!--             <el-button type="danger"  @click="deleteCare(scope.row.careId)"></el-button>-->
              <el-popconfirm title="确定删除吗？" @confirm="deleteCare(scope.row.careId)">
              <el-button slot="reference" type="danger" icon="el-icon-delete" style="margin-left: 5px"></el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--分页查询-->
<!--    <div style="margin-top: 10px">-->
<!--      <el-pagination-->
<!--          @size-change="handleSizeChange"-->
<!--          @current-change="handleCurrentChange"-->
<!--          :current-page="params.pageNum"-->
<!--          :page-sizes="[5, 10, 15, 20]"-->
<!--          :page-size="params.pageSize"-->
<!--          layout="total, sizes, prev, pager, next"-->
<!--          :total="total">-->
<!--      </el-pagination>-->
<!--    </div>-->
    <!--新增&编辑用户表单-->
    <div>
      <el-dialog title="请填写护理信息" :visible.sync="dialogFormVisible" width="40%">
        <el-form :model="form">
          <el-form-item label="用户id">
            <el-input v-model="form.userId" disabled autocomplete="off" style="width: 80%"></el-input>
          </el-form-item>
          <el-form-item label="类型">
            <el-input v-model="form.type" autocomplete="off" style="width: 80%"></el-input>
          </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitCareInfo">确 定</el-button>
          <el-button @click="dialogFormVisible = false">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>


import request from "@/utils/request";

export default {
  data() {
    return {
      params: {
        id: '',
        username: '',
        pageNum: 1,
        pageSize: 5
      },
      params2: {
        id: '',
        username: '',
        pageNum: 1,
        pageSize: 5
      },
      tableData: [],
      total: 0,
      dialogFormVisible: false,
      form: {
      },
      user: sessionStorage.getItem("user") ? JSON.parse(sessionStorage.getItem("user")) : {}
    }
  },
  created() {
    this.findCareByUserId()
    this.getUser().then(res => {
      this.form = res
    })

  },
  methods: {
    async getUser() {
      return  this.user.id
      // return (await request.get("/user/search/self/1")).data
    },
    //按用户名查询
    findCareByUserId() {
      console.log(this.user.id)
      request.get("/care/search/" + this.user.id).then(res => {
        if (res.code === '0') {
          this.tableData = res.data;
          this.total = res.data.total;
        } else {
          this.$message({
            message: res.msg,
            type: 'error'
          });
        }
      })
    },
    findSelfInfoByCareID() {
      request.get("/care/search/self/" + this.params2.id).then(res => {
        if (res.code === '0') {
          console.log(res);
          this.tableData = res.data;
          this.total = 1;
        } else {
          this.$message({
            message: res.msg,
            type: 'error'
          });
        }
      })
    },
    //
    submitCareInfo() {
      request.post("/care/add",this.form).then(res => {
        if (res.code === "0") {
          this.$message({
            message: '操作成功',
            type: 'success'
          });
          this.dialogFormVisible = false;
          this.findCareByUserId();
        } else {
          this.$message({
            message: res.msg,
            type: 'error'
          });
        }
      })
    },
    //清空
    reset() {
      // this.params = {
      //   pageNum: 1,
      //   pageSize: 5,
      //   username: ''
      // }
      this.findCareByUserId()
    },
    //改变每页显示条数
    handleSizeChange(pageSize) {
      this.params.pageSize = pageSize;
      this.findPageByUserName();
    },
    //改变当前在第几页
    handleCurrentChange(pageNum) {
      this.params.pageNum = pageNum;
      this.findPageByUserName();
    },
    addCare() {
      this.dialogFormVisible = true;
      this.form = {};
      this.form.userId = this.user.id;
    },
    editCare(obj) {
      this.form = obj;
      this.dialogFormVisible = true;
    },
    deleteCare(id) {
      request.delete("/care/delete/" + id).then(res => {
        if (res.code === "0") {
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.dialogFormVisible = false;
          this.findCareByUserId();
        } else {
          this.$message({
            message: res.msg,
            type: 'error'
          });
        }
      })
    },
    submitUserInfo() {
      request.post("/user/save",this.form).then(res => {
        if (res.code === "0") {
          this.$message({
            message: '操作成功',
            type: 'success'
          });
          this.dialogFormVisible = false;
          this.findPageByUserName();
        } else {
          this.$message({
            message: res.msg,
            type: 'error'
          });
        }
      })

    }
  }
}
</script>
